from django.shortcuts import render
from rest_framework.generics import GenericAPIView,mixins
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from apps.areas.models import Area
from apps.areas.serializers import AreaInfoSerializer, AreaSubsInfoSerializer


class AreaInfoView(CacheResponseMixin,ReadOnlyModelViewSet):

    pagination_class = None
    #重写
    def get_queryset(self):
        if self.action=="list":
            return Area.objects.filter(parent__isnull=True).all()
        else:
            return Area.objects.filter().all()

    # 重写
    def get_serializer_class(self, *args, **kwargs):
        if self.action=="list":
            return AreaInfoSerializer
        else:
            return AreaSubsInfoSerializer

